package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.GoldenResultAdapter;
import com.glory.dgcpcpda.adapter.GoldenSmapleAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.GoldenSampleTestDataEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.PageInfoEntity;
import com.glory.dgcpcpda.entity.ParametersEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class GoldenSmapleActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flMark)
    FrameLayout flMark;
    @BindView(R.id.ivMark)
    ImageView ivMark;
    @BindView(R.id.rvGoldenSmapleList)
    RecyclerView rvGoldenSmapleList;
    @BindView(R.id.rvGoldenResultSmapleList)
    RecyclerView rvGoldenResultSmapleList;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.etTestRecID)
    FreeUI_ClearEditText etTestRecID;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvCustomerCode)
    TextView tvCustomerCode;
    @BindView(R.id.tvWaferMaterialDesc)
    TextView tvWaferMaterialDesc;
    @BindView(R.id.tvCircuitName)
    TextView tvCircuitName;
    @BindView(R.id.tvMark)
    TextView tvMark;


    private FreeUI_GeneralPop mMarkPop;
    private ArrayList<String> markList = new ArrayList<>();

    private ArrayList<String> testDataList = new ArrayList<>();
    private GoldenSmapleAdapter mGoldenSmapleAdapter;
    private com.glory.dgcpcpda.adapter.GoldenResultAdapter mGoldenResultAdapter;
    private ArrayList<GoldenSampleTestDataEntity.DataEntity> dataEntities;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_golden_smaple;
    }

    @Override
    protected void initView() {
        tvTitle.setText("GoldenSample验证");
        setMainLayout(llMain);

        initEdit(etEqpID, s -> LotUtils.searchEqpInfo(s, GoldenSmapleActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
            @Override
            public void onSuccess(EqpInfoEntity response) {
                tvEqpType.setText(response.getEQUIPMENT().getEQPTYPE());

            }

            @Override
            public void onFail(Throwable t) {
                loadError(t,"EQPQUERY");
            }
        }));

        initEdit(etLot, s -> {
            if(etEqpID.getText().toString().isEmpty()){
                showToast("请先扫描设备ID",false);
                return;
            }
            //获取lot信息
            LotUtils.searchLotInfo(GoldenSmapleActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    tvCustomerCode.setText(response.getLOT().getCUSTOMERCODE());
                    tvWaferMaterialDesc.setText(response.getCHIPNAME());
                    tvCircuitName.setText(response.getPRODUCTMODEL());
                    getTestRecList();
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });
        });

        initEdit(etTestRecID, s ->{
            FreeUI_EntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>>
                    (new TypeToken<BaseResponseBean<GoldenSampleTestDataEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                        ArrayList<String> strings = new ArrayList<>();
                        for (GoldenSampleTestDataEntity.GoldenSampleCfgEntity data:response.body().getResponse().getBody().getGoldenSampleCfgs()){
                            strings.add(data.getSampleNO());
                        }
                        mGoldenSmapleAdapter.setDataList(strings);
                        mGoldenSmapleAdapter.setNewData(response.body().getResponse().getBody().getDatas());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(),"GetGoldenSampleTestData");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("TestRecID", s);
            NetUtils.requestPostNet("CPC.GetGoldenSampleTestData", "GetGoldenSampleTestData", this, map, callBack);

        });
    }


    private void getTestRecList(){

        FreeUI_EntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>>
                (new TypeToken<BaseResponseBean<GoldenSampleTestDataEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
//                    ArrayList<String> strings = new ArrayList<>();
//                    for (GoldenSampleTestDataEntity.GoldenSampleCfgEntity data:response.body().getResponse().getBody().getGoldenSampleCfgs()){
//                        strings.add(data.getSampleNO());
//                    }
//                    mGoldenSmapleAdapter.setDataList(strings);
//                    mGoldenSmapleAdapter.setNewData(response.body().getResponse().getBody().getDatas());
                    dataEntities = response.body().getResponse().getBody().getData();
                    testDataList.clear();
                    for (GoldenSampleTestDataEntity.DataEntity datum : dataEntities) {
                        testDataList.add(datum.getTesterRupt());
                    }


                }
            }

            @Override
            public void onError(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"getTestData");
            }
        };


        JsonObject map = new JsonObject();
        PageInfoEntity pageInfo = new PageInfoEntity();
        ParametersEntity parameters = new ParametersEntity(etLot.getText().toString(),etEqpID.getText().toString());
        map.add("PageInfo", new JsonParser().parse(new Gson().toJson(pageInfo)).getAsJsonObject());
        map.add("Parameters", new JsonParser().parse(new Gson().toJson(parameters)).getAsJsonObject());
        NetUtils.requestPostNet("getTestData", "GetTestData", this, map, callBack);
    }
    @Override
    protected void initData() {
        initRecyclerView(rvGoldenSmapleList, this);
        mGoldenSmapleAdapter = new GoldenSmapleAdapter(null,this,getSupportFragmentManager());
        mGoldenSmapleAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mGoldenSmapleAdapter.isFirstOnly(true);
        rvGoldenSmapleList.setAdapter(mGoldenSmapleAdapter);

        initRecyclerView(rvGoldenResultSmapleList,this);
        mGoldenResultAdapter = new GoldenResultAdapter(null,this,getSupportFragmentManager());
        mGoldenResultAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mGoldenResultAdapter.isFirstOnly(true);
        rvGoldenResultSmapleList.setAdapter(mGoldenResultAdapter);

    }

    @OnClick({R.id.tvBaseSubmit,R.id.flMark})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (actionControl("Submit11")) {
                    return;
                }

                if (getText(etLot).equals("")){
                    showToast("批次号不能为空",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>>
                                (new TypeToken<BaseResponseBean<GoldenSampleTestDataEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    rvGoldenSmapleList.setVisibility(View.GONE);
                                    rvGoldenResultSmapleList.setVisibility(View.VISIBLE);
                                    mGoldenResultAdapter.setNewData(response.body().getResponse().getBody().getDatas());
                                    showToast("提交成功",true);
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"GoldenSampleTestDataCheck");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.add("Datas", new JsonParser().parse(new Gson().toJson(mGoldenSmapleAdapter.getData())).getAsJsonArray());
                        for (GoldenSampleTestDataEntity.DataEntity dataEntity : dataEntities) {
                            if (dataEntity.getTesterRupt().equals(tvMark.getText().toString())){
                                map.addProperty("TestRecID", dataEntity.getTestRecId());
                            }
                        }
//                        map.addProperty("TestRecID", tvMark.getText().toString());
                        NetUtils.requestPostNet("GoldenSampleTestDataCheck", "GoldenSampleTestDataCheck", this, map, callBack);
                    }
                });
                break;
            case R.id.flMark:
                if (mMarkPop != null) {
                    if (mMarkPop.isShowing()) {
                        mMarkPop.dismiss();
                    }
                } else {
                    mMarkPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flMark.getWidth(), testDataList, ivMark, (codeS, i) -> {
                        tvMark.setText(codeS);
                        FreeUI_EntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<GoldenSampleTestDataEntity>>
                                (new TypeToken<BaseResponseBean<GoldenSampleTestDataEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    ArrayList<String> strings = new ArrayList<>();
                                    for (GoldenSampleTestDataEntity.GoldenSampleCfgEntity data:response.body().getResponse().getBody().getGoldenSampleCfgs()){
                                        strings.add(data.getSampleNO());
                                    }
                                    mGoldenSmapleAdapter.setDataList(strings);
                                    mGoldenSmapleAdapter.setNewData(response.body().getResponse().getBody().getDatas());
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<GoldenSampleTestDataEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"GetGoldenSampleTestData");
                            }
                        };
                        JsonObject map = new JsonObject();
                        for (GoldenSampleTestDataEntity.DataEntity dataEntity : dataEntities) {
                            if (dataEntity.getTesterRupt().equals(codeS)){
                                map.addProperty("TestRecID", dataEntity.getTestRecId());
                            }
                        }

                        NetUtils.requestPostNet("CPC.GetGoldenSampleTestData", "GetGoldenSampleTestData", this, map, callBack);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivMark);
                mMarkPop.showAsDropDown(flMark, 0, 0);
                break;

        }

    }
}
